package com.qf.service.serviceimpl;

import com.qf.dao.UserDao;
import com.qf.dao.daoimpl.UserDaoImpl;
import com.qf.pojo.PageBean;
import com.qf.pojo.User;
import com.qf.service.UserService;
import com.qf.utils.MailUtils;

import java.sql.SQLException;
import java.util.List;

/**
 * @author Mr.Liu
 * @date 2021/11/19 10:26
 */

public class UserServiceImpl implements UserService {
    /**
     * 查找所有用户业务接口实现层
     * */
    @Override
    public List<User> findAllUser() {
        try {
            UserDao ud = new UserDaoImpl();//调用数据访问层获取数据
            List<User> user = ud.findAllUser();
            if(user != null){
                return user;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }
    /**
     * 通过id查找用户业务接口实现层
     * */
    @Override
    public User findById(String uid) {
        try {
            UserDao ud = new UserDaoImpl();
            User user1 = ud.selectById(uid);
            if (user1 != null){
                return user1;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }
    /**
     * 修改用户业务接口实现层
     * */
    @Override
    public void modifyUser(User user) {
        try {
            UserDao ud = new UserDaoImpl();
            ud.modifyUser(user);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
    /**
     * 删除用户业务接口实现层
     * */
    @Override
    public void deleteUser(String uid) {
        UserDao ud = new UserDaoImpl();
        try {
            ud.deleteUser(uid);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
    /**
     * 分页查询用户业务实现层
     * */
    @Override
    public PageBean<User> FindUserByPage(int currentPage, int pageSize) {
        try {
            UserDao ud = new UserDaoImpl();
            int totalSize = ud.FindCount();
            List<User> users = ud.FindUser(currentPage,pageSize);
            PageBean<User> ub = new PageBean<>(currentPage,pageSize,totalSize,users);
            return ub;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }
    /*
    *校验用户名业务实现层
    * */
    @Override
    public boolean checkUser(String username) {
        try {
            UserDao ud = new UserDaoImpl();
            User user = ud.checkUser(username);
            if(user != null){
                return true;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return false;
    }
    /*
    * 注册用户业务实现
    * */
    @Override
    public void registerUser(User user) {
        try {
            UserDao ud = new UserDaoImpl();
            ud.registerUser(user);
            String content = "欢迎您,请<a href='http://localhost:8080/maven_study_war/user?methodName=activeUser&code="+user.getCode()+"'>激活</a>";
            MailUtils.sendMail(user.getEmail(),content,"用户激活");
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    @Override
    public User findUserByCode(String code) {
        User user = null;
        try {
            UserDao ud = new UserDaoImpl();
            user = ud.findUserByCode(code);
            if(user == null){
                return null;
            }
            user.setState(1);
            ud.updateUser(user);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return user;
    }

    /**
     *通过用户名查找用户
     * @param username 用户名
     * @param password 密码
     * @return 返回用户
     */
    @Override
    public User loginUser(String username, String password) {
        try {
            UserDao ud = new UserDaoImpl();
            User user = ud.selectUser(username,password);
            if(user != null){
                return user;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }
}
